﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace LFractal
{
	/// <summary>
	/// Interaction logic for LEntityEditor.xaml
	/// </summary>
	public partial class LEntityEditor : Window
	{

        private LFractalLib.Entity entity = null;
        BindingList<LSystemProductionRule> rules = new BindingList<LSystemProductionRule>();
        LFractalMainWindow parentWindow = null;

		public LEntityEditor(LFractalMainWindow p )
		{
            
			this.InitializeComponent();
            parentWindow = p;
			// Insert code required on object creation below this point.
            this.Loaded += new RoutedEventHandler(LEntityEditor_Loaded);
            this.Closing += new CancelEventHandler(LEntityEditor_Closing);
            
		}

        void LEntityEditor_Closing(object sender, CancelEventArgs e)
        {
            parentWindow = null;
        }

        void LEntityEditor_Loaded(object sender, RoutedEventArgs e)
        {
            System.IO.DirectoryInfo oDir = new System.IO.DirectoryInfo(LFractalMainWindow.StorageFolder);
            System.IO.FileInfo[] oFiles = oDir.GetFiles("*.xml");
            txtName.Text = "MyTree" + ((oFiles != null) ? oFiles.Length.ToString() : "0");
            listviewProductionRules.ItemsSource = rules;
            btnAddRule_Click(null, null);
        }

		private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			this.Close();
		}

		private void mainWindow_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			this.DragMove();
		}

		private void btnRender_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            /* first construct the Entity definition */
            entity = new LFractalLib.Entity(txtName.Text);
            entity.Axiom = txtAxiom.Text;

            foreach (LSystemProductionRule R in rules)
            {
                entity.ProductionRules.Add(new LFractalLib.ProductionRule(R.NonTerminal, R.Rule));
            }

            string[] coordi = txtOrigin.Text.Split(",".ToCharArray());
            entity.X0 = double.Parse(coordi[0]);
            entity.Y0 = double.Parse(coordi[1]);
            entity.ArmScalingFactor = double.Parse(txtScalingFactor.Text);
            entity.ArmLength = double.Parse(txtLength.Text);
            entity.Alpha = double.Parse(txtAlpha.Text);
            entity.Delta = double.Parse(txtDelta.Text);

			LEntityRenderer renderer = new LEntityRenderer(entity, this, parentWindow);
            renderer.Show();
            renderer.DrawEntity();
			
		}

        private void btnAddRule_Click(object sender, RoutedEventArgs e)
        {
            rules.Add(new LSystemProductionRule('F', "F"));
        }

        
	}
}